from sql_operation import MySqlOperation


class LoginHandle:
    def __init__(self, user_name, passwd, conn=None):
        self.conn = conn
        self.uname = user_name
        self.pwd = passwd
        self.sql_conn = MySqlOperation()

    def login_handle(self, **kwargs):
        print('处理请求信息')
        sql = 'select %s from %s where %s=\'%s\' and %s=\'%s\'' \
              % (kwargs['col'], kwargs['tbname'], kwargs['cond_name'],
                 self.uname, kwargs['cond_pwd'], self.pwd)
        self.sql_conn.operation(sql)
        sqlr = self.sql_conn.fetch_all()

        if not sqlr:
            print('登录请求：404')
            self.conn.send('404:用户名密码不存在'.encode())

        else:
            success_rep = '200:OK:' + str(sqlr[0][0])
            # print('登录请求：200')
            print(success_rep)
            self.conn.send(success_rep.encode())

    def teacher_login_handle(self):
        self.login_handle(col='teacher_id', tbname='teacher_info', cond_name='teacher_name',
                          cond_pwd='teacher_pwd')

    def student_login_handle(self):
        self.login_handle(col='stu_id', tbname='stu_info', cond_name='stu_name',
                          cond_pwd='stu_pwd')


if __name__ == '__main__':
    l = LoginHandle('dht', '123456')
    code = l.login_handle()
    print(code)